package threading;

/**
 * @author 徐志荣
 * @date 2023/3/16 8:53
 */
public class ThreadDemo10 {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            //currentThread() : 获取当前线程实例
            //此处,currentThread() 得到的对象就是 t
            //isInterrupted() t对象里 自带的一个标志位  false
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    break;
                }
            }
        });

        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //把 t 内部的标志位设置为 true
        thread.interrupt();
    }
}
